package com.yofoto.lyy.core.common.constant.factory;

import com.baomidou.mybatisplus.plugins.Page;
import com.yofoto.lyy.core.common.constant.state.Order;
import com.yofoto.lyy.core.support.HttpKit;
import com.yofoto.lyy.core.util.ToolUtil;


import javax.servlet.http.HttpServletRequest;

/**
 *
 *分页参数构建
 * @author fengshuonan
 * @date 2017-04-05 22:25
 */
public class PageFactory<T> {

    /**
     * BootStrap Table默认的分页参数创建
     * @return
     */
    public Page<T> bootStrapTablePage() {
        HttpServletRequest request = HttpKit.getRequest();
        int limit = Integer.valueOf(request.getParameter("limit"));     //每页多少条数据
        int offset = Integer.valueOf(request.getParameter("offset"));   //每页的偏移量(本页当前有多少条)
        String sort = request.getParameter("sort");         //排序字段名称
        String order = request.getParameter("order");       //asc或desc(升序或降序)
        if (ToolUtil.isEmpty(sort)) {
            Page<T> page = new Page<>((offset / limit + 1), limit);
            page.setOpenSort(false);
            return page;
        } else {
            Page<T> page = new Page<>((offset / limit + 1), limit, sort);
            if (Order.ASC.getDes().equals(order)) {
                page.setAsc(true);
            } else {
                page.setAsc(false);
            }
            return page;
        }
    }

    /**
     * dataTables 默认分页参数创建
     * @return
     */
    public Page<T> dataTablesPage(){
        HttpServletRequest request = HttpKit.getRequest();
        //从第几条记录开始
        int start = Integer.valueOf(request.getParameter("start"));
        //每页显示多少条记录
        int length = Integer.valueOf(request.getParameter("length"));
        //datatables查询次数的标示位置
        int draw = Integer.valueOf(request.getParameter("draw"));
        return new Page<>(start/length+1,length);
    }
}
